package com.dxj.dog.vaccine.service.impl;

import java.util.Date;
import java.util.List;

import com.dxj.common.utils.DateUtils;
import com.dxj.common.utils.SecurityUtils;
import com.dxj.dog.admin.domain.DogAdmin;
import com.dxj.dog.admin.service.IDogAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dxj.dog.vaccine.mapper.DogVaccineMapper;
import com.dxj.dog.vaccine.domain.DogVaccine;
import com.dxj.dog.vaccine.service.IDogVaccineService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 疫苗记录Service业务层处理
 *
 * @author 杜晓俊
 * @date 2022-03-29
 */
@Service
public class DogVaccineServiceImpl implements IDogVaccineService {
    @Resource
    private DogVaccineMapper dogVaccineMapper;

    /**
     * 查询疫苗记录
     *
     * @param vaccineId 疫苗记录主键
     * @return 疫苗记录
     */
    @Override
    public DogVaccine selectDogVaccineByVaccineId(Long vaccineId) {
        return dogVaccineMapper.selectDogVaccineByVaccineId(vaccineId);
    }

    /**
     * 查询疫苗记录列表
     *
     * @param dogVaccine 疫苗记录
     * @return 疫苗记录
     */
    @Override
    public List<DogVaccine> selectDogVaccineList(DogVaccine dogVaccine) {
        return dogVaccineMapper.selectDogVaccineList(dogVaccine);
    }

    @Autowired
    private IDogAdminService dogAdminService;

    /**
     * 新增疫苗记录
     *
     * @param dogVaccine 疫苗记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDogVaccine(DogVaccine dogVaccine) {
        if (dogVaccine.getDogId() == null) {
            throw new RuntimeException("未检测到dogId，请联系管理员！");
        }
        //新增的时候，顺便修改狗狗仓库表
        DogAdmin dogAdmin = new DogAdmin();
        dogAdmin.setDogId(dogVaccine.getDogId());
        dogAdmin.setVaccineNumbers((long) dogVaccineMapper.selectDogVaccineByDogId(dogVaccine.getDogId()).size() + 1);
        dogAdmin.setVersion(dogAdminService.selectDogAdminByDogId(dogVaccine.getDogId()).getVersion());
        dogAdminService.updateDogAdmin(dogAdmin);
        dogVaccine.setCreateBy(SecurityUtils.getUsername());
        dogVaccine.setCreateTime(DateUtils.getNowDate());
        return dogVaccineMapper.insertDogVaccine(dogVaccine);
    }

    /**
     * 修改疫苗记录
     *
     * @param dogVaccine 疫苗记录
     * @return 结果
     */
    @Override
    public int updateDogVaccine(DogVaccine dogVaccine) {
        dogVaccine.setUpdateTime(DateUtils.getNowDate());
        return dogVaccineMapper.updateDogVaccine(dogVaccine);
    }

    /**
     * 批量删除疫苗记录
     *
     * @param vaccineIds 需要删除的疫苗记录主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteDogVaccineByVaccineIds(Long[] vaccineIds) {
        //删除几个，数量就减去几
        updateVaccineByDogId(vaccineIds[0], vaccineIds.length);
        return dogVaccineMapper.deleteDogVaccineByVaccineIds(vaccineIds);
    }

    /**
     * 删除疫苗记录信息
     *
     * @param vaccineId 疫苗记录主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteDogVaccineByVaccineId(Long vaccineId) {
        //删除1个，数量就减去1
        updateVaccineByDogId(vaccineId, 1);
        return dogVaccineMapper.deleteDogVaccineByVaccineId(vaccineId);
    }

    /**
     * 疫苗数量拨乱反正
     *
     * @param vaccineId 疫苗Id，通过它获得哪条狗
     * @param length    疫苗数量
     */
    private void updateVaccineByDogId(Long vaccineId, Integer length) {
        DogVaccine dogVaccine = dogVaccineMapper.selectDogVaccineByVaccineId(vaccineId);
        DogAdmin dogAdmin = dogAdminService.selectDogAdminByDogId(dogVaccine.getDogId());
        dogAdmin.setVaccineNumbers(dogAdmin.getVaccineNumbers() - length);
        dogAdminService.updateDogAdmin(dogAdmin);
    }
}
